//
//  ImageProcessViewController.m
//  ImageWorkshop
//
//  Created by Runli Song on 2023/11/23.
//  Copyright © 2023 James Song. All rights reserved.
//

#import "ImageProcessViewController.h"
#import "YQImageTool.h"

@interface ImageProcessViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic, strong) UIView *titleView;

@property (nonatomic, strong) UIImageView *mainIamge;

@property (nonatomic, strong) UIButton *waterImage;
@property (nonatomic, strong) UILabel *paramDesc1;
@property (nonatomic, strong) UITextField *paramField;
@property (nonatomic, strong) UILabel *paramDesc2;
@property (nonatomic, strong) UITextField *paramField2;
@property (nonatomic, strong) UILabel *paramDesc3;
@property (nonatomic, strong) UITextField *paramField3;

@property (nonatomic, strong) UIButton *doneBtn;
@property (nonatomic, strong) UIButton *saveBtn;


@end

@implementation ImageProcessViewController
{
    UILabel *_titleLabel;
    UIImage *_waterImg;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:self.titleView];
    

    
    [self.view addSubview:self.doneBtn];
    [self.doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(20));
        make.bottom.equalTo(@(-50));
        make.width.equalTo(@(kScreenWidth/2 - 30));
        make.height.equalTo(@(50));
    }];
    [self.view addSubview:self.saveBtn];
    [self.saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-20));
        make.bottom.width.height.equalTo(self.doneBtn);
    }];
    
    [self.view addSubview:self.paramDesc3];
    [self.paramDesc3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.doneBtn);
        make.bottom.equalTo(self.doneBtn.mas_top).offset(-20);
        make.width.equalTo(@(80));
        make.height.equalTo(@(30));
    }];
    
    [self.view addSubview:self.paramField3];
    [self.paramField3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-20));
        make.left.equalTo(self.paramDesc3.mas_right).offset(10);
        make.top.bottom.equalTo(self.paramDesc3);
    }];
    
    [self.view addSubview:self.paramDesc2];
    [self.paramDesc2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.paramDesc3);
        make.bottom.equalTo(self.paramDesc3.mas_top).offset(-20);
    }];
    [self.view addSubview:self.paramField2];
    [self.paramField2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-20));
        make.left.equalTo(self.paramDesc2.mas_right).offset(10);
        make.top.bottom.equalTo(self.paramDesc2);
    }];
    
    [self.view addSubview:self.paramDesc1];
    [self.paramDesc1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.paramDesc2);
        make.bottom.equalTo(self.paramDesc2.mas_top).offset(-20);
        make.height.equalTo(@(30));
    }];
    [self.view addSubview:self.paramField];
    [self.paramField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-20));
        make.left.equalTo(self.paramDesc1.mas_right).offset(10);
        make.top.bottom.equalTo(self.paramDesc1);
    }];
    
    [self.view addSubview:self.waterImage];
    [self.waterImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.paramDesc1);
        make.right.equalTo(self.paramField);
        make.bottom.equalTo(self.paramDesc1.mas_top).offset(-20);
        make.height.equalTo(@(200));
    }];

    [self.view addSubview:self.mainIamge];
    [self.mainIamge mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleView.mas_bottom).offset(20);
        make.left.right.equalTo(self.waterImage);
        make.bottom.equalTo(self.waterImage.mas_top).offset(-20);
    }];
}

- (void)onClickWater{
    UIImagePickerController * picker = [[UIImagePickerController alloc]init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];}

- (void)onClickClose{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)onClickSave{
    [UIImage saveImageToPhotoAlbum:self.mainIamge.image];
}

- (void)onClickDone{
    switch (_actionType) {
        case ActionWater:
            
            _mainIamge.image = [YQImageTool GetWaterPrintedImageWithBackImage:_image
                                                                andWaterImage:_waterImg
                                                                       inRect:CGRectFromString([NSString stringWithFormat:@"{{%@}, {%@}}",_paramField.text, _paramField2.text])
                                                                        alpha:_paramField3.text.floatValue
                                                                   waterScale:NO];
            
            
            break;
        case ActionShadow:
            _mainIamge.image = [YQImageTool creatShadowImageWithOriginalImage:self.image
                                                              andShadowOffset:CGSizeMake(0, 0)
                                                                 andBlurWidth:_paramField.text.integerValue
                                                                     andAlpha:_paramField2.text.integerValue
                                                                     andColor:UIColorFromHex([self hexIntegerFromHexString:_paramField3.text])];
            break;
        case ActionRotate:
            _mainIamge.image = [YQImageTool GetRotationImageWithImage:self.image 
                                                                Angle:self.paramField.text.integerValue];
            break;
        case ActionCilp:
            _mainIamge.image = [YQImageTool cutImageWithImage:self.image 
                                                      atPoint:CGPointFromString([NSString stringWithFormat:@"{%@}", _paramField.text])
                                                     withSize:CGSizeFromString([NSString stringWithFormat:@"{%@}", _paramField2.text])
                                              backgroundColor:UIColorFromHex([self hexIntegerFromHexString:_paramField3.text])];
            break;
        case ActionCorner:
            _mainIamge.image = [YQImageTool getCornerImageAtOriginalImageCornerWithImage:self.image
                                                                           andCornerWith:self.paramField.text.integerValue
                                                                      andBackGroundColor:[UIColor clearColor]];
            break;
        default:
            self.title = @"加水印";
            break;
    }
}

#pragma mark - delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage * image = info[@"UIImagePickerControllerOriginalImage"];
    
    [_waterImage setImage:image forState:UIControlStateNormal];
    _waterImg = image;
    
}

#pragma mark - setter
- (void)setImage:(UIImage *)image{
    _image = image;
    self.mainIamge.image = image;
}

- (void)setActionType:(ActionType)actionType{
    _actionType = actionType;
    
    switch (_actionType) {
        case ActionWater:
            self.title = @"加水印";
            [self.paramDesc1 setText:@"水印坐标："];
            [self.paramDesc2 setText:@"水印宽高："];
            [self.paramDesc3 setText:@"透明度："];
            self.paramField.placeholder = @"例：100,100";
            self.paramField2.placeholder = @"例：500,500";
            self.paramField3.placeholder = @"0.5";
            break;
        case ActionShadow:
            self.title = @"加阴影";
            [self.waterImage mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(0));
            }];
            
            [self.paramDesc1 setText:@"模糊度："];
            [self.paramDesc2 setText:@"透明度："];
            [self.paramDesc3 setText:@"颜色："];
            self.paramField.placeholder = @"例：100";
            self.paramField2.placeholder = @"例：0.9";
            self.paramField3.placeholder = @"#ff0000";
            
            break;
        case ActionRotate:
            self.title = @"旋转";
            [self.waterImage mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(0));
            }];
            [self.paramDesc2 mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(0));
            }];
            [self.paramDesc3 mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(0));
            }];
            self.paramField2.hidden = YES;
            self.paramField3.hidden = YES;

            [self.paramDesc1 setText:@"旋转角度:"];
            self.paramField.placeholder = @"例：180";
         
            break;
        case ActionCilp:
            self.title = @"裁剪";
            [self.waterImage mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(0));
            }];
            
            [self.paramDesc1 setText:@"切割坐标："];
            [self.paramDesc2 setText:@"切割宽高："];
            [self.paramDesc3 setText:@"填充色："];

            self.paramField.placeholder = @"例：100,100";
            self.paramField2.placeholder = @"例：500,500";
            self.paramField3.placeholder = @"#ff0000";
            break;
        case ActionCorner:
            self.title = @"切圆角";
            [self.waterImage mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(0));
            }];
            [self.paramDesc2 mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(0));
            }];
            [self.paramDesc3 mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(0));
            }];
            self.paramField2.hidden = YES;
            self.paramField3.hidden = YES;
            [self.paramDesc1 setText:@"圆角半径："];
            self.paramField.placeholder = @"例：100";

            break;
        default:
            self.title = @"加水印";
            break;
    }
}

#pragma mark - getter
- (UIView *)titleView{
    if(!_titleView){
        _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
        _titleView.backgroundColor = UIColor.clearColor;
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
        _titleLabel.textColor = UIColor.blackColor;
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.center = _titleView.center;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = self.title;
        [_titleView addSubview:_titleLabel];
        
        UIButton *close = [UIButton buttonWithType:UIButtonTypeClose];
        close.frame = CGRectMake(15, 5, 40, 40);
        close.backgroundColor = UIColor.clearColor;
        [close addTarget:self action:@selector(onClickClose) forControlEvents:UIControlEventTouchUpInside];
        [_titleView addSubview:close];
        
        
    }
    return _titleView;
}

- (UIImageView *)mainIamge{
    if(!_mainIamge){
        _mainIamge = [[UIImageView alloc] init];
        _mainIamge.contentMode = UIViewContentModeScaleAspectFit;
        _mainIamge.backgroundColor = UIColorFromHex(0xf2f2f2);
    }
    return _mainIamge;
}

- (UIButton *)waterImage{
    if(!_waterImage){
        _waterImage = [UIButton buttonWithType:UIButtonTypeCustom];
        _waterImage.imageView.contentMode = UIViewContentModeScaleAspectFit;
        _waterImage.backgroundColor = UIColorFromHex(0xf2f2f2);
        [_waterImage addTarget:self action:@selector(onClickWater) forControlEvents:UIControlEventTouchUpInside];
        [_waterImage setTitle:@"选择水印图片" forState:UIControlStateNormal];
        
    }
    return _waterImage;
}
- (UILabel *)paramDesc1{
    if(!_paramDesc1){
        _paramDesc1 = [[UILabel alloc] init];
        _paramDesc1.textColor = UIColor.blackColor;
        _paramDesc1.font = [UIFont systemFontOfSize:14];
    }
    return _paramDesc1;
}
- (UITextField *)paramField{
    if(!_paramField){
        _paramField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
        _paramField.backgroundColor = UIColorFromHex(0xf2f2f2);
        _paramField.layer.cornerRadius = 4;
    }
    return _paramField;
}
- (UILabel *)paramDesc2{
    if(!_paramDesc2){
        _paramDesc2 = [[UILabel alloc] init];
        _paramDesc2.textColor = UIColor.blackColor;
        _paramDesc2.font = [UIFont systemFontOfSize:14];
    }
    return _paramDesc2;
}
- (UITextField *)paramField2{
    if(!_paramField2){
        _paramField2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
        _paramField2.backgroundColor = UIColorFromHex(0xf2f2f2);
        _paramField2.layer.cornerRadius = 4;
    }
    return _paramField2;
}
- (UILabel *)paramDesc3{
    if(!_paramDesc3){
        _paramDesc3 = [[UILabel alloc] init];
        _paramDesc3.textColor = UIColor.blackColor;
        _paramDesc3.font = [UIFont systemFontOfSize:14];
    }
    return _paramDesc3;
}
- (UITextField *)paramField3{
    if(!_paramField3){
        _paramField3 = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
        _paramField3.backgroundColor = UIColorFromHex(0xf2f2f2);
        _paramField3.layer.cornerRadius = 4;
    }
    return _paramField3;
}

- (UIButton *)doneBtn{
    if(!_doneBtn){
        _doneBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_doneBtn setTitle:self.title forState:UIControlStateNormal];
        [_doneBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _doneBtn.backgroundColor = MainThemeColor;
        _doneBtn.layer.cornerRadius = 10;
        _doneBtn.layer.masksToBounds = YES;
        [_doneBtn addTarget:self action:@selector(onClickDone) forControlEvents:UIControlEventTouchUpInside];
    }
    return _doneBtn;
}

- (UIButton *)saveBtn{
    if(!_saveBtn){
        _saveBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_saveBtn setTitle:@"保存图片" forState:UIControlStateNormal];
        [_saveBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _saveBtn.layer.cornerRadius = 10;
        _saveBtn.layer.masksToBounds = YES;
        _saveBtn.backgroundColor = MainThemeColor;
        [_saveBtn addTarget:self action:@selector(onClickSave) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}

- (NSInteger)hexIntegerFromHexString:(NSString *)hexString {
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    unsigned int hexValue = 0;
    [scanner scanHexInt:&hexValue];
    return hexValue;
}

@end
